﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using QuanLyTruong.Business;
using QuanLyTruong;

namespace QuanlyMakichhoat
{
    public partial class frmQuanlymakichhoat : Form
    {
        public frmQuanlymakichhoat()
        {
            InitializeComponent();
            Module.Connect();
        }

        int CDKeyID = 0;

        private void btnTimkiem_Click(object sender, EventArgs e)
        {
            lblMess.Text = "";
            List<CDKeys> lstCDKey = new CDKeys().CDKeyAll();
            try
            {
                var objCDKey = (from result in lstCDKey
                                where result.CDKey == txtCDKey.Text
                                select result).First();
                CDKeyID = objCDKey.ID;
                lblCDKey.Text = objCDKey.CDKey;
                txtMakhoitao.Text = objCDKey.Makhoitao;
                GetSplitKey(objCDKey.Makichhoat);
            }
            catch (Exception ex) 
            {
                lblMess.Text = "Không tìm thấy CDKey, vui lòng nhập lại";
            }

            //if (File.Exists(Application.StartupPath + "\\CDkey.xml"))
            //{
            //    XmlDocument xd = new XmlDocument();
            //    xd.Load(Application.StartupPath + "\\CDkey.xml");

            //    XmlNode xNode = xd.SelectSingleNode("root/key[@CDKey='" + txtCDKey.Text + "']");
            //    if (xNode != null)
            //    {
            //        lblCDKey.Text = xNode.Attributes["CDKey"].Value;
            //        txtMakhoitao.Text = xNode.Attributes["Makhoitao"].Value;
            //        txtMakichhoat.Text = xNode.Attributes["Makichhoat"].Value;
            //    }
            //    else
            //    {
            //        lblMess.Text = "Không tìm thấy CDKey, vui lòng nhập lại";
            //    }
            //}
            //else
            //{
            //    lblMess.Text = "Không tìm thấy CDKey, vui lòng nhập lại";
            //}
        }

        private void btnSinhmakichhoat_Click(object sender, EventArgs e)
        {
            lblMess.Text = "";
            if (txtMakhoitao.Text.Length == 16 && lblCDKey.Text.Length == 12)
            {
                GetSplitKey(GenerateActiveNumber(txtMakhoitao.Text, lblCDKey.Text));
                CDKeys insertItem = new CDKeys();
                insertItem.CDKey = lblCDKey.Text;
                insertItem.Makhoitao = txtMakhoitao.Text;
                insertItem.Makichhoat = SetSplitKey();
                insertItem.ID = CDKeyID;
                insertItem.Update();
                lblMess.Text = "DONE";
                ////Luu lai
                //if (File.Exists(Application.StartupPath + "\\CDkey.xml"))
                //{
                //    XmlDocument xd = new XmlDocument();
                //    xd.Load(Application.StartupPath + "\\CDkey.xml");

                //    XmlNode xNode = xd.SelectSingleNode("root/key[@CDKey='" + lblCDKey.Text + "']");
                //    xNode.Attributes["Makhoitao"].Value = txtMakhoitao.Text;
                //    xNode.Attributes["Makichhoat"].Value = txtMakichhoat.Text;

                //    xd.Save(Application.StartupPath + "\\CDkey.xml");
                //}
            }
            else
            {
                MessageBox.Show("Mã khởi tạo phải có đủ 16 chữ số và không có khoảng trắng,\r\n CD Key phải có 12 chữ số và không có khoảng trắng \r\n Vui lòng kiểm tra.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public static string GenerateActiveNumber(string ActiveKeyRaw, string CDKey)
        {
            string ActiveKey = CDKey + ActiveKeyRaw + "+-*:";
            string newcode = "";
            for (int i = 0; i < ActiveKey.Length; i = i + 2)
            {
                int charnumber = (int)ActiveKey[i] + (int)ActiveKey[i + 1];
                int oNumber = 0;
                GetOneDigigNumberFromOtherNumber(charnumber, out oNumber);
                newcode += oNumber.ToString();
            }
            return newcode;
        }

        public static void GetOneDigigNumberFromOtherNumber(int iNumber, out int oNumber)
        {
            if (iNumber.ToString().Length > 1)
            {
                int temp = 0;
                for (int i = 0; i < iNumber.ToString().Length; i++)
                {
                    temp += Convert.ToInt32(iNumber.ToString()[i].ToString());
                }
                oNumber = temp;
                GetOneDigigNumberFromOtherNumber(oNumber, out oNumber);
            }
            else
            {
                oNumber = iNumber;
                return;
            }
        }

        private void GetSplitKey(string key)
        {
            if (key == "")
            {
                txtMakichhoat1.Text = "";
                txtMakichhoat2.Text = "";
                txtMakichhoat3.Text = "";
                txtMakichhoat4.Text = "";
            }
            else
            {
                txtMakichhoat1.Text = key.Substring(0, 4);
                txtMakichhoat2.Text = key.Substring(4, 4);
                txtMakichhoat3.Text = key.Substring(8, 4);
                txtMakichhoat4.Text = key.Substring(12, 4);
            }
        }

        private string SetSplitKey()
        {
            return txtMakichhoat1.Text + txtMakichhoat2.Text + txtMakichhoat3.Text + txtMakichhoat4.Text;
        }
    }
}
